package com.avic.map.controller;

import com.avic.map.common.constant.SystemLogConstant;
import com.avic.map.common.constant.UserConstant;
import com.avic.map.common.convertor.UavResourceConvertor;
import com.avic.map.common.util.AssertUtil;
import com.avic.map.common.util.ex.BizErrorCode;
import com.avic.map.common.util.lang.BaseResponse;
import com.avic.map.common.util.lang.ServiceCallBack;
import com.avic.map.controller.request.CreateUavResourceReq;
import com.avic.map.controller.request.GetUavResourceListReq;
import com.avic.map.controller.request.UpdateUavResourceReq;
import com.avic.map.controller.result.BaseCreateResourceRes;
import com.avic.map.controller.result.GetUavResourceListRes;
import com.avic.map.dal.condition.PageQueryUavResourceListCondition;
import com.avic.map.dal.model.UavResource;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@Slf4j
@RestController
@RequestMapping("resource/uav")
@Api(tags = "无人机资源", description = "分页查询无人机资源列表&创建无人机资源&修改无人机资源&删除无人机资源")
public class UavResourceController extends AbstractFacadeImpl {

    @ApiOperation(value = "分页查询无人机资源列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "getUavResourceListReq", required = true, dataType = "GetUavResourceListReq")
    })
    @PostMapping("list")
    public GetUavResourceListRes getUavResourceList(@RequestBody GetUavResourceListReq getUavResourceListReq) {
        final GetUavResourceListRes result = new GetUavResourceListRes();
        return serviceExecuteTemplate.executeService(result, new ServiceCallBack() {
            @Override
            public void check() throws Exception {
            }

            @Override
            public void doService() throws Exception {
                PageQueryUavResourceListCondition pageQueryUavResourceListCondition = new PageQueryUavResourceListCondition(
                        getUavResourceListReq.getCurrentPage(), getUavResourceListReq.getPageSize()
                );
                pageQueryUavResourceListCondition.setUavName(getUavResourceListReq.getUavName());
                pageQueryUavResourceListCondition.setUavNumber(getUavResourceListReq.getUavNumber());

                List<UavResource> uavResources = uavResourceService.itemList(pageQueryUavResourceListCondition);

                result.setItemList(UavResourceConvertor.uavResourceListVOToUavResourceListInfo(uavResources));
                result.setItemTotal(uavResourceService.queryRecordsTotal(pageQueryUavResourceListCondition));
                result.setSuccess(true);
            }
        });
    }

    @ApiOperation(value = "创建无人机资源")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "createUavResourceReq", required = true, dataType = "CreateUavResourceReq")
    })
    @PostMapping
    public BaseCreateResourceRes createCarResource(@RequestBody CreateUavResourceReq createUavResourceReq,
                                                   @RequestHeader(value = UserConstant.HEADER_CURRENT_USER_ID_KEY, required = false) String currentUserId) {
        final BaseCreateResourceRes result = new BaseCreateResourceRes();
        return serviceExecuteTemplate.executeService(result, new ServiceCallBack() {
            @Override
            public void check() throws Exception {
                AssertUtil.isNotBlank(createUavResourceReq.getUavName(), "uavName is blank");
                AssertUtil.isNotBlank(createUavResourceReq.getUavNumber(), "uavNumber is blank");

            }

            @Override
            public void doService() throws Exception {

                UavResource uavResource = new UavResource();
                uavResource.setUavName(createUavResourceReq.getUavName());
                uavResource.setUavNumber(createUavResourceReq.getUavNumber());
                uavResource.setUavRtsp(createUavResourceReq.getUavRtsp());
                result.setItemId(String.valueOf(uavResourceService.create(uavResource)));

                // 新建系统日志
                systemServiceFeignService.createSystemLog(
                        String.format(SystemLogConstant.CREATE_UAV_RESOURCE, result.getItemId(),
                                objectMapper.writeValueAsString(createUavResourceReq)), currentUserId);
                result.setSuccess(true);
            }
        });
    }

    @ApiOperation(value = "修改资源无人机")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "updateUavResourceReq", required = true, dataType = "UpdateUavResourceReq")
    })
    @PutMapping
    public BaseResponse updateCarResource(@RequestBody UpdateUavResourceReq updateUavResourceReq,
                                          @RequestHeader(value = UserConstant.HEADER_CURRENT_USER_ID_KEY, required = false) String currentUserId) {
        final BaseResponse result = new BaseResponse();
        return serviceExecuteTemplate.executeService(result, new ServiceCallBack() {
            @Override
            public void check() throws Exception {
                AssertUtil.isTrue(updateUavResourceReq.getItemId() > 0, BizErrorCode.PARAM_ERROR, "修改ID必须大于零");
                AssertUtil.isNotBlank(updateUavResourceReq.getUavName(), "uavName is blank");
                AssertUtil.isNotBlank(updateUavResourceReq.getUavNumber(), "uavNumber is blank");
            }

            @Override
            public void doService() throws Exception {

                UavResource uavResource = new UavResource();
                uavResource.setUavName(updateUavResourceReq.getUavName());
                uavResource.setUavNumber(updateUavResourceReq.getUavNumber());
                uavResource.setUavRtsp(updateUavResourceReq.getUavRtsp());
                uavResource.setId(updateUavResourceReq.getItemId());

                uavResourceService.update(uavResource);

                // 新建系统日志
                systemServiceFeignService.createSystemLog(
                        String.format(SystemLogConstant.UPDATE_UAV_RESOURCE, uavResource.getId(),
                                objectMapper.writeValueAsString(updateUavResourceReq)), currentUserId);
                result.setSuccess(true);
            }
        });
    }

    @ApiOperation(value = "删除无人机资源")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "itemId", paramType = "path", value = "资源ID", required = true)
    })
    @DeleteMapping("/{itemId}")
    public BaseResponse deleteCarResource(@PathVariable("itemId") int itemId,
                                          @RequestHeader(value = UserConstant.HEADER_CURRENT_USER_ID_KEY, required = false) String currentUserId) {
        final BaseResponse result = new BaseResponse();
        return serviceExecuteTemplate.executeService(result, new ServiceCallBack() {
            @Override
            public void check() throws Exception {
                AssertUtil.isTrue(itemId > 0, BizErrorCode.PARAM_ERROR, "数据ID大于零");

            }

            @Override
            public void doService() throws Exception {

                uavResourceService.delete(itemId);

                // 新建系统日志
                systemServiceFeignService.createSystemLog(
                        String.format(SystemLogConstant.DELETE_UAV_RESOURCE, itemId), currentUserId);
                result.setSuccess(true);
            }
        });
    }
}
